<?php

namespace app\controller\api;

use app\controller\ApiBaseController;
use app\model\LawUserModel;
use app\model\PointsLogModel;
use think\response\Json;

class Operate extends ApiBaseController
{
    /**
     * 积分变动操作
     *
     * @return Json
     */
    public function points(): Json
    {
        try {
            // 接收参数
            $strIdCode = param_filter($this->request->param('id_code', ''), 'string');
            if (empty($strIdCode)) {
                return api_response([], '缺少用户唯一识别码', 0);
            }
            $intModifyType = param_filter($this->request->param('modify_type', ''), 'int');
            if (empty($intModifyType)) {
                return api_response([], '缺少积分变动类型', 0);
            }
            $intModifyCount = param_filter($this->request->param('modify_count', ''), 'int');
            if (empty($intModifyCount)) {
                return api_response([], '缺少积分变动数量', 0);
            }
            $intModifyReason = param_filter($this->request->param('modify_reason', ''), 'int');
            if (empty($intModifyReason)) {
                return api_response([], '缺少积分变动原因', 0);
            }

            //确认用户
            $arrLawUserInfo = LawUserModel::getOneByWhere([['id_code', '=', $strIdCode]]);
            if (empty($arrLawUserInfo)) {
                return api_response([], '用户不存在', 0);
            }
            $arrData = [
                'modify_type' => $intModifyType,
                'modify_count' => $intModifyCount,
                'modify_reason' => $intModifyReason
            ];

            $boolModify = (new PointsLogModel())->pointsModify($arrLawUserInfo, $arrData);
            if (!$boolModify) {
                return api_response([], '积分变动失败', 0);
            }
            return api_response();
        } catch (\Exception $e) {
            return api_response([], '积分变动失败：' . $e->getMessage(), 0);
        }
    }
}
